//
//  CYCreditTabBarVC.m
//  CreditAddressBook
//
//  Created by LE on 15/12/30.
//  Copyright © 2015年 Lee. All rights reserved.
//

#import "CYCreditTabBarVC.h"
#import "LEMDLib.h"
#import "CYCreditVController.h"
#import "CYJumpsTool.h"
#import "CYPostedOnlineVC.h"
#import "CYCreditTool.h"
#import "MBProgressHUD+MJ.h"
#import "CYShowPayBackTableVC.h"
#import "LEBarButtonItem.h"
#import "LEAlertView+LE.h"
#import "CYLoginHttpTool.h"
#import "CYReleaseVC.h"
#import "CYPersonalDetailsVController.h"
#import "CYReleaseTypeVC.h"
#import "CYHelpViewController.h"
#import "CYJsonInfo.h"
#import "NSObject+MJKeyValue.h"
#import "CYPersonReleaseVC.h"


@interface CYCreditTabBarVC ()<UISearchBarDelegate,UIWebViewDelegate>

@property (nonatomic, strong)UIWebView *webview;
@property (nonatomic, strong)UIView *hiview;
@property (nonatomic, assign)BOOL isfirstLoad;
@property (nonatomic, strong)NSString *url1;

@property(nonatomic, strong) NSString *networkName;
@property(nonatomic, weak) UIButton *cover;
@property(nonatomic, weak) UISearchBar *searchbar;
@end

@implementation CYCreditTabBarVC

- (NSString *)networkName {
    if (!_networkName) {
        _networkName = @"网络曝光";
    }
    return _networkName;
}

- (void) viewWillAppear:(BOOL)animated{
     
}

- (void)viewDidLoad {
    [super viewDidLoad];
     
     self.view.backgroundColor = CYColor(242, 241, 244, 1);
     //store版本备用 信用查询是否需要
//    [CYHttpTool postWithURL:[CYURL shared].getCredit params:@{} success:^(id json) {
//          CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
//          NSString *str = jsoninfo.message;
//          if (str.intValue == 1) {
//               //不展示信用系统
//               CYLog(@"notshow");
//               [self setupwebview];
//               
//          }else{
//               CYLog(@"show");
     
//     http://192.168.1.104:8100/#/tab/dash
//     _webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen ]bounds].size.width,[[UIScreen mainScreen ]bounds].size.height)];
//     _webview.backgroundColor = CYColor(68, 68, 68, 1);
//     _webview.delegate = self;
//     NSString *url= @" http://192.168.1.104:8100/#/tab/dash";
//     NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:url]];
//     [_webview loadRequest:request];
//     [self.view addSubview:_webview];
               [self loadData];
               [self setupTabBar];
               [self setupSearchBar];
               [self setupNavigationView];
//          }
//          
//     } failure:^(NSError *error) {
//          CYLog(@"error = %@",error);
//     }];
  
}
-(void)setupwebview{
    UIView *zhezhaoView = [[UIView alloc]init];
    zhezhaoView.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[ UIScreen mainScreen ] bounds].size.height);
    zhezhaoView.backgroundColor = [UIColor blueColor];
    UILabel *lab  = [[UILabel alloc]init];
    lab.frame = zhezhaoView.frame;
    lab.backgroundColor = [UIColor whiteColor];
    lab.text = @"没有新的数据";
     lab.textAlignment = NSTextAlignmentCenter;
    
    [zhezhaoView addSubview:lab];
    [self.view addSubview:zhezhaoView];
}

- (void)setupSearchBar {
//    CGFloat width = CYScreeSize.width - 150;
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.width, 50)];
//    UIColor *color =  self.navigationController.navigationBar.barTintColor;
    [titleView setBackgroundColor:[UIColor clearColor]];
    UISearchBar *searchBar = [[UISearchBar alloc] init];
    searchBar.delegate = self;
    searchBar.frame = CGRectMake(15, 7, self.view.width-30, 35);
    searchBar.backgroundColor = [UIColor blueColor];
    searchBar.layer.cornerRadius = 10;
    searchBar.layer.masksToBounds = YES;
    [searchBar.layer setBorderWidth:8];
    [searchBar.layer setBorderColor:[UIColor whiteColor].CGColor];  //设置边框为白色
    searchBar.placeholder = @"姓名/电话号码/身份证号";
     
    [titleView addSubview:searchBar];
    self.searchbar = searchBar;
    
    [self.navigationItem.titleView sizeToFit];
     [self.view addSubview:titleView];
     
}

- (void)setupTabBar {
    //rightBarButtonItme
//    LEBarButtonItem *more = [[LEBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ice_tab_user_nomal"] andSelImage:nil andTitle:@"我的发布" target:self action:@selector(more)];
     
     LEBarButtonItem *more = [[LEBarButtonItem alloc] initWithImagelong:[UIImage imageNamed:@"icon_wyfb(1)"] andSelImage:nil target:self action:@selector(more)];
    self.navigationItem.rightBarButtonItem = more;
    
    //tabBar
    CYCreditVController *creditVC = [CYCreditVController le_loadFromStoryBoard:[CYJumpsTool mainSB]];
    CYPostedOnlineVC *posted = [[CYPostedOnlineVC alloc] init];
    CYShowPayBackTableVC *show = [[CYShowPayBackTableVC alloc] init];
    [LEMDLib tabBarWithController:self subControllers:@[posted, creditVC, show] subControllerNames:@[self.networkName, @"权威发布", @"还款展示"]];
    //遮盖
    UIButton *cover = [[UIButton alloc] initWithFrame:self.view.frame];
    [self.view addSubview:cover];
    self.cover = cover;
    cover.backgroundColor = [UIColor colorWithWhite:.4 alpha:.8];
    cover.alpha = 0;
    [cover addTarget:self action:@selector(coverClick) forControlEvents:UIControlEventTouchUpInside];
}
- (void)more {
     CYPersonReleaseVC *releace = [[CYPersonReleaseVC alloc] init];
     releace.hidesBottomBarWhenPushed = YES;
     [self.navigationController pushViewController:releace animated:YES];
     
}

- (void)coverClick {
    [self.searchbar resignFirstResponder];
    [UIView animateWithDuration:.5 animations:^{
        self.cover.alpha = 0;
    }];
}

- (void)loadData {
    [CYCreditTool getAuditNetworkCollectRecordNumSuccess:^(NSNumber *total) {
        self.networkName = CYStringFormat(@"网络曝光(%@)", total);
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"服务器连接错误"];
    }];
}

#pragma mark - 违约发布
- (void)releaseClick {
    CYUser *user = [CYLoginHttpTool userData].user;
    if (user) {
        CYReleaseTypeVC *releaseType = [[CYReleaseTypeVC alloc] init];
        releaseType.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:releaseType animated:YES];
    } else {
        [CYJumpsTool jumpToLogin:self];
    }
}

#pragma mark - UISearchBarDelegate
/**
 *  键盘搜索按键监听
 */
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
    [UIView animateWithDuration:.5 animations:^{
        self.cover.alpha = 0;
    }];
    //发出通知网络曝光与权威发布进行搜索
    if (searchBar.text && !searchBar.text.isEmpty) {
        [CYNotificationCenter postNotificationName:nCYCreditSearch object:nil userInfo:@{kCYCreditSearch: searchBar.text}];
    }
}
/**
 *  键盘弹出:搜索框开始编辑文字
 */
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    [UIView animateWithDuration:.5 animations:^{
        self.cover.alpha = .7;
    }];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
    [self coverClick];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
    [self coverClick];
    searchBar.text = nil;
}

- (void)dealloc {
    CYLog(@"信用系统销毁");
}
@end
